package com.lvmama.common.exception;

import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lvmama.common.result.CodeMessage;
import com.lvmama.common.result.Result;

/**
 * Created by Administrator on 2018/8/6.
 */
@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public Result<String> exceptionHandler(Exception e){

        if(e instanceof GlobalException){
            GlobalException ex = (GlobalException)e;
            return Result.error(ex.getCodeMessage());
        } else{
            log.error("errorMessage:{}", e.getMessage(), e);
            return Result.error(CodeMessage.SERVER_ERROR);
        }
    }
}
